package com.sinoeyes.controller.system;

import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.constant.UserConstants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.core.page.RestPageResult;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysDictTypeFilter;
import com.sinoeyes.system.service.ISysDictTypeService;
import com.sinoeyes.system.vo.SysDictTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/type")
@Api(value = "数据字典类型信息 API", tags = {"数据字典信息"})
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("查询字典类型分页数据")
    public Result<RestPageResult<SysDictTypeVo>> list(SysDictTypeFilter dictTypeFilter)
    {
        RestPageResult<SysDictTypeVo> pageResult = dictTypeService.selectDictTypeList(dictTypeFilter);
        return Result.success(pageResult);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    @ApiOperation("查询字典类型详细")
    public Result<SysDictTypeVo> getInfo(@PathVariable Long dictId)
    {
        return Result.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增字典类型")
    public Result<Integer> add(@RequestBody SysDictTypeVo dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(), "新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return Result.success(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改字典类型")
    public Result<Integer> edit(@RequestBody SysDictTypeVo dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(), "修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictId}")
    @ApiOperation("删除字典类型")
    public Result<Integer> remove(@PathVariable Long dictId)
    {
        return Result.success(dictTypeService.deleteDictTypeById(dictId));
    }
}
